var $form;
var form;
var $;
layui.config({
	base : "../../js/"
}).use(['form','layer','upload','laydate'],function(){
	form = layui.form();
	var layer = parent.layer === undefined ? layui.layer : parent.layer;
		$ = layui.jquery;
		$form = $('form');
		laydate = layui.laydate;
	var id = window.sessionStorage.getItem("paramaterEditId");	
    layui.upload({
    	elem: '.userFaceBtn', //绑定元素
    	url : $.httpRootPath() + "/api/sys/qq/picture",
  		type: 'images',
         success: function(res){
         	if(res.code != "00"){
         		layer.msg("服务器异常，请稍候再试");
         	}else{
         		layer.msg("修改成功");
         		$("#userFace").attr("src",res.data);
         		$(".parValue").val(res.data);
         	}
  		},
         error: function(){
         	 console.log("失败"); 
    	}
    });
    var index = layer.load(1, {
		shade: [0.1, '#fff']
	});
	var data ={
		id:id
	}
	layer.close(index);
    getInfo(data,function(){
    	layer.close(index);
    });

	function fillData(data){
	 
	 	$(".parKey").val(data.parKey); //用户名
	   	$(".parValue").val(data.parValue); //出生年月
        $(".parType").val(data.parType); //
        $(".sort").val(data.sort); //手机号
        form.render();
	}

    //提交个人资料
    form.on("submit(changeUser)",function(data){
    	var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var data = {
            'parValue' : $(".parValue").val(),
            'parType' : $(".parType").val(),
            'parKey' : $(".parKey").val(),
            'sort' : $(".sort").val(),
            'id':id
        };
   		updateInfo(data,function(){
   	    	layer.close(index);
   	    	top.layer.close(index);
 			layer.closeAll("iframe");
	 		//刷新父页面
	 		parent.location.reload();
  		 })
  		 return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })

    
  function getInfo(data,callback) {
	$.ajax({
		url: $.httpRootPath() + "/api/sys/parameter/"+data.id,
		type: "get",
		dataType: "json",
		data: data,
		success: function(data) {
			if(data.code == "00") {
				fillData(data.data);
			} else {
				layer.msg(data.msg);
				return;
			}
		},
		error: function() {
			layer.msg("加载失败");
		},
		complete: function() {
			callback();
		}
	})
}
})
function updateInfo(data, callback) {
	data._method = 'PUT';
	$.ajax({
		url: $.httpRootPath() + '/api/sys/parameter/'+data.id,
		type: 'POST',
		dataType: 'json',
		data: data,
		timeout: 15000,
		cache: false,
		success: function(r) {
			if(r.code == "00") {
				layer.msg("增加成功！");
			} else {
				layer.msg(r.msg);
			}
		},
		error: function(e) {
			layer.msg("系统错误！");
		},
		complete: function() {
			callback();
		}
	});
	}
